var _UrlPrefix = "http://www.fuzhuangchangongxiao.cn/";
const _AppAccess = web_access;

(function ($) {
    // 登录/注册
    var login = {
        init: function () {
            var vm = new Vue({
                el: "#forgetBox",

                // 数据
                data: {
                    phone: null,
                    password: null,
                    code: null,
                    codeType: true,     // 获取验证码
                    codeText: '获取验证码', 
                    codeBtnClass: 'code-not-exist',
                    isCheck: true,     // 校验账号密码是否一致
                },

                // 周期
                mounted: function () {

                },

                watch: {
                    phone: function (newValue, oldValue) {
                        this._checkUserPhone(oldValue, newValue)
                    }
                },

                computed: {
                    codeTextFun: function () {
                        return this.codeText;
                    }
                },

                // 方法
                methods: {
                    onGetCode () {
                        // 获取验证码
                        var self = this;
                        if(!this.phone || this.phone.length != 11) {
                            return alert("请输入正确的手机号码");
                        }
                        $.ajax({
                            type:"POST",
                            url: _UrlPrefix + "/api/member/yzm",
                            data: {
                                phone: self.phone,
                                app_access: _AppAccess
                            },
                            success:function(res){
                                if(res.code == 1) {
                                    if (self.codeType) {
                                        self.codeType = false
                                        self.codeBtnClass = 'code-exist'
                                        self._second = 60
                                        self.codeText = self._second + "s后重置"
                                        // 设置倒计时
                                        self.t1 = setInterval(function () {
                                            if (self._second > 0) {
                                                self._second--;
                                                self.codeText = self._second + "s后重置"
                                            } else {
                                                self.codeType = true;
                                                self.codeBtnClass = 'code-not-exist'
                                                self.codeText = "获取验证码"
                                                clearInterval(self.t1)
                                            }
                                        }, 1000)
                                    }
                                }
                            },
                            error: function(xhr,status,error) {
                                // console.log("xhr:", xhr)
                                // console.log("status:", status)
                                // console.log("error:", error)
                            },
                            complete: function () {

                            }
                        })
                        // 获取验证码
                        // if (this.codeType) {
                        //     var self = this;
                        //     self.codeType = false
                        //     self.codeBtnClass = 'code-exist'
                        //     self._second = 60
                        //     self.codeText = self._second + "s后重置"
                        //     // 设置倒计时
                        //     this.t1 = setInterval(function () {
                        //         if (self._second > 0) {
                        //             self._second--;
                        //             self.codeText = self._second + "s后重置"
                        //         } else {
                        //             self.codeType = true;
                        //             self.codeBtnClass = 'code-not-exist'
                        //             self.codeText = "获取验证码"
                        //             clearInterval(self.t1)
                        //         }
                        //     }, 1000)
                        // }
                    },
                    _checkUserPhone (oldValue, newValue) {
                        if(!newValue || newValue == null) return
                        if(newValue.length > 11) {
                            this.phone = oldValue
                            return
                        }
                        for(var i = 0;i < newValue.length;i++) {
                            if(newValue[i] < '0' || newValue[i] > '9') {
                                this.phone = oldValue
                            }
                        }              
                    },
                    onBack () {
                        // 返回上一级
                        history.back(-1)
                    },
                    onLogin () {
                        let self = this
                        // 用户用验证码登录
                        $.ajax({
                            type:"POST",
                            url: _UrlPrefix + "api/web/user/login-code",
                            data: {
                                phone: self.phone,
                                code: self.code,
                                app_access: _AppAccess
                            },
                            success:function(res){
                                if(res.code == 200) {
                                    alert("登录成功")
                                    console.log("res: ", res)
                                }else {
                                    self.isCheck = false
                                    setTimeout(function () {
                                        self.isCheck = true
                                    }, 1500)
                                }
                            },
                            error: function(xhr,status,error) {
                                // console.log("xhr:", xhr)
                                // console.log("status:", status)
                                // console.log("error:", error)
                            },
                            complete: function () {

                            }
                        })
                    }
                },

                // 周期
                created: function () {
                    // 页面初始化接口请求
                },
            })
        }
    }
    login.init()
})(jQuery)